

/*##############################################################################
 Thomas M. Flaherty
 Email: t1flaher@ucsd.edu
 Date Created: 07/09/2019
 Comment(s):
		(i) Replication of Model 2 in Table 3 of GRR (2018) 
 Input File(s): 
		(1) GRR_replicationdata.dta
 Output File(s): 
		(1) NA
##############################################################################*/


clear    
clear matrix
set matsize 10000
set more off
capture log close 
set scrollbufsize 300000 // (set scrollbufsize will take effect the next time you launch Stata)


*###############################################################################
* change directory and import "raw" data 
*###############################################################################

cd "~/Dropbox/replication_files/
use data/input/GRR_replicationdata.dta


*###############################################################################
* TABLE
*###############################################################################

   
pantob PRR   Lgini_disp_inc Lunemp Ldiff_immigration D_par nuts_2 
estimates store m1, title("(1)")
gen interaction = Ldiff_immigration*Lgini_disp_inc  	
pantob PRR   Lgini_disp_inc Lunemp Ldiff_immigration interaction D_par nuts_2 
estimates store m2, title("(2)")	

estout m1 m2 , starlevels( * 0.10 ** 0.05 *** 0.010) cells(b(star fmt(3)) se(par fmt(2)))legend label varlabels(_cons Constant) ///
   stats(N r2_a df_r, fmt(0 3 0) label(N Adj.R-sqr df.resid)) title("") wrap dropped    ///
   nobaselevels       
 

*###############################################################################
* Coeff plot
*###############################################################################

* converge to standardized units for visual comparability.   
egen zLdiff_immigration = std(Ldiff_immigration)
egen zLgini_disp_inc = std(Lgini_disp_inc)
egen zLunemp = std(Lunemp)
gen zinteraction = zLdiff_immigration*zLgini_disp_inc   

label var zLdiff_immigration "Change immigration"
label var zLgini_disp_inc "Inequality Gini"
label var zLunemp "Unemployment rate"
label var D_par "Parliamentary election = 1"
label var zinteraction "Inequality Gini x Change immigration"

pantob PRR  D_par zLunemp zLgini_disp_inc zLdiff_immigration  nuts_2  
estimates store GRR2018
pantob PRR  D_par zLunemp zLgini_disp_inc zLdiff_immigration zinteraction  nuts_2 
estimates store IO2020
coefplot GRR2018 IO2020, drop(_cons) xline(0) 	///
	graphregion(color(white)) ///
	keep(zLgini_disp_inc zLdiff_immigration zinteraction zLunemp D_par) ///
	scheme(s2mono)
graph export "figures/GRR_coefplot.png", replace

*###############################################################################
* Marginal Effects Plots 
*###############################################################################

* Notes: 
*		see http://www.nicholastdavis.com/wp-content/uploads/2018/12/davis_better_graphs.pdf for examples that overlay histograms. 
*		For an alternative, see https://journals.sagepub.com/doi/pdf/10.1177/1536867X1401400115. 

* Linear effects
tobit  PRR  zLunemp  c.Lgini_disp_inc##c.zLdiff_immigration D_par  i.nuts_2, ll(0) 
margins, dydx(zLdiff_immigration) at(Lgini_disp_inc = (.23(0.01).38 )) // 
marginsplot, /// base command
	yline(0, lpattern(dash) lcolor(black))  ///
	legend(off) ///
	level(95) /// CI
	recastci(rspike) /// CI display options
	ci1opts(lcolor(black)) /// CI color
	plotregion(fcolor(white) lcolor(gs10) lwidth(med)) /// background color and border outline
	plot1opts(mcolor(white) mlcolor(black) lcolor(black)) ///
	xscale(range(.21 .39) ) /// 
	xlabel(.21(0.01).39, labcolor(gs4) tlcolor() tlwidth(thin) labsize(small) nogrid) /// x-axis labels coloration 
	ylabel(, labcolor(gs4) tlcolor() tlwidth(thin) labsize(small) nogrid angle(horizontal)) ///
	xtitle("Inequality Gini", color(black)) /// 
	ytitle("Effect of immigration on populist vote share", color(black)) ///
	title("") ///
		addplot(hist Lgini_disp_inc, percent /// Use "discrete" if actual distribution is desired
			blcolor(black) fcolor(black) ///
			yaxis(2) /// creates a second y-axis
			yscale(alt lcolor() axis(2)) /// 
			ylabel(0 " " 20 " " 40 " " 60 " " 80 " " 100 " ", /// minimizing the height of histogram 
				labcolor() axis(2) tlcolor(black) tlwidth(thin) labsize(small) tl(0)) /// label options for 2nd axis and removes ticks 
			ytitle("", axis(2)) /// no title on 2nd axis 
			legend(off) ///
		)
graph export "figures/GRR_marginsplot_linear.png", replace

* Non-linear effects
tobit  PRR  zLunemp  c.Lgini_disp_inc##c.Lgini_disp_inc##c.zLdiff_immigration D_par  i.nuts_2, ll(0) 
margins, dydx(zLdiff_immigration) at(Lgini_disp_inc = (.23(0.01).38 )) // 
marginsplot, /// base command
	yline(0, lpattern(dash) lcolor(black))  ///
	legend(off) ///
	level(95) /// CI
	recastci(rspike) /// CI display options
	ci1opts(lcolor(black)) /// CI color
	plotregion(fcolor(white) lcolor(gs10) lwidth(med)) /// background color and border outline
	plot1opts(mcolor(white) mlcolor(black) lcolor(black)) ///
	xscale(range(.21 .39) ) /// 
	xlabel(.21(0.01).39, labcolor(gs4) tlcolor() tlwidth(thin) labsize(small) nogrid) /// x-axis labels coloration 
	ylabel(, labcolor(gs4) tlcolor() tlwidth(thin) labsize(small) nogrid angle(horizontal)) ///
	xtitle("Inequality Gini", color(black)) /// 
	ytitle("Effect of immigration on populist vote share", color(black)) ///
	title("") ///
		addplot(hist Lgini_disp_inc, percent /// Use "discrete" if actual distribution is desired
			blcolor(black) fcolor(black) ///
			yaxis(2) /// creates a second y-axis
			yscale(alt lcolor() axis(2)) /// 
			ylabel(0 " " 20 " " 40 " " 60 " " 80 " " 100 " ", /// minimizing the height of histogram 
				labcolor() axis(2) tlcolor(black) tlwidth(thin) labsize(small) tl(0)) /// label options for 2nd axis and removes ticks 
			ytitle("", axis(2)) /// no title on 2nd axis 
			legend(off) ///
		)
graph export "figures/GRR_marginsplot_nonlinear.png", replace

*###############################################################################
* End
*###############################################################################




		
		
	
